package com.sduept.nwld.dataserver.controller.base.primary;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sduept.bigdata.web.jsf.controller.AbstractController;

import javax.annotation.PostConstruct;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 主监控controller
 *
 */
@Named
@ViewScoped
public class MapViewController extends AbstractController {
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1422827621384190320L;


	
	@PostConstruct
	private void init(){
	}
	
	public void openLineFault(){
		String selectName = getParameter("selectName");
		//log.info(selectName);
		Map<String,Object> options = new HashMap<String, Object>();
        options.put("resizable", false);
        options.put("draggable", true);
		options.put("contentWidth", "800");
        options.put("modal", false);
        Map<String,List<String>> params = new HashMap<>();
        List<String> ps = new ArrayList<>();
        try {
			ps.add(URLEncoder.encode(selectName,"utf-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
        params.put("lineName", ps);
//        RequestContext.getCurrentInstance().showMessageInDialog(new FacesMessage("test"));
        String url = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath()+"ui/map/dialog/lineFaultDialog";
        openDialog("/ui/map/dialog/lineFaultDialog", params, options);
	}
	public void openDialog(){
		String url = getParameter("url");
		String contentWidth = getParameter("contentWidth");
		String contentHeight = getParameter("contentHeight");
		contentWidth = contentWidth == null ? "800":contentWidth;
		contentHeight = contentHeight == null ? "600":contentHeight;
		String params = getParameter("params");
		
		Map<String,Object> options = new HashMap<String, Object>();
		options.put("resizable", true);
		options.put("draggable", true);
		options.put("contentWidth", contentWidth);
		options.put("contentHeight", contentHeight);
		options.put("modal", false);
		
		Map<String,List<String>> mapparams = new HashMap<>();
		if(params != null){
			JSONArray jarrays = JSONArray.parseArray(params);
			if(null != jarrays){
				for (int i = 0; i < jarrays.size(); i++) {
					JSONObject o = (JSONObject) jarrays.get(i);
					String pv = (o.getString("value"));
					if(!org.apache.commons.lang3.StringUtils.isEmpty(pv)){
						List<String> p = new ArrayList<>();
//						try {
//							p.add(URLEncoder.encode(pv,"utf-8"));
//						} catch (UnsupportedEncodingException e) {
//							e.printStackTrace();
//						}
						p.add(pv);
						mapparams.put(o.getString("name"), p);
					}
				}
			}
		}
		openDialog(url, mapparams,options );
	}
}
